<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/08/0008
 * Time: 16:38
 */

namespace app\sports_admin\service;

use app\exception\SchoolYearException;
use app\sports_admin\model\SchoolYear as SchoolYearModel;
use think\Request;

class SchoolYear
{
    /*
     * 添加学年信息
     */
    public static function insert()
    {
        $info = self::getInfo();
        $bool = SchoolYearModel::insertSchoolYearInfo($info);
        if(!$bool){
            throw new SchoolYearException([
                'msg'=>'添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'添加成功','code'=>201]);
    }
    /*
     * 修改学年信息
     * @param $id 学年信息id
     */
    public static function update()
    {
        $id = input('post.id');
        $info = self::getInfo();
        $bool = (new SchoolYearModel())->save($info,['id'=>$id]);
        if(!$bool){
            throw new SchoolYearException([
                'msg'=>'修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'修改成功','code'=>201]);
    }
    /*
     * 删除学年信息
     * @param $id 学年信息id
     */
    public static function delete()
    {
        $id = input('post.id');
        $bool = SchoolYearModel::destroy($id);
        if(!$bool){
            throw new SchoolYearException([
                'msg'=>'删除失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'删除成功','code'=>201]);
    }
    /*
     * 获取学年信息
     */
    private static function getInfo()
    {
        //year 学年  last_semester上学期  next_semester下学期
        return Request::instance()->only(['year','last_semester','next_semester']);
    }
}